package com.hyts.apidoc.swagger.factory.markup;

import com.hyts.apidoc.swagger.config.SwaggerConfig;
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.apidoc.swagger.factory
 * author:Libo/Alex
 * create-date:2019-05-11 20:43
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: Swagger2Markup工程方法操作类
 */
public class Swagger2MarkupFactory {



    Swagger2MarkupConfig swagger2MarkupConfig;

    /**
     * @param language
     * @param markupLanguage
     * @return
     */
    public Swagger2MarkupFactory create(Language language,MarkupLanguage markupLanguage){
        swagger2MarkupConfig = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(markupLanguage == null ? MarkupLanguage.ASCIIDOC:markupLanguage)
                .withOutputLanguage(language == null ? Language.ZH:language)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();
        return this;
    }

    /**
     * @param language
     * @return
     */
    public Swagger2MarkupFactory create(Language language){
       return create(language,null);
    }

    /**
     * @param markupLanguage
     * @return
     */
    public Swagger2MarkupFactory create(MarkupLanguage markupLanguage){
        return create(null,markupLanguage);
    }

    /**
     * @return
     */
    public Swagger2MarkupFactory create(){
        return create(null,null);
    }

    /**
     * @param localSwaggerFilePath
     * @param outputFilePath
     * @return
     */
    public Swagger2MarkupFactory converter(String localSwaggerFilePath,String outputFilePath){
        Swagger2MarkupConverter converter = Swagger2MarkupConverter
                .from(Paths.get(localSwaggerFilePath))
                .withConfig(swagger2MarkupConfig)
                .build();
        converter.toFile(Paths.get(outputFilePath));
        return this;
    }

    /**
     * @param localSwaggerFilePath
     * @return
     */
    public Swagger2MarkupFactory converter(String localSwaggerFilePath){
        converter(localSwaggerFilePath,SwaggerConfig.OUTPUT_FILE_PATH);
        return this;
    }

    /**
     * @param outputFilePath
     * @return
     */
    public Swagger2MarkupFactory converter(Path outputFilePath){
        converter(SwaggerConfig.INPUT_SWAGGER_FILE_PATH,SwaggerConfig.OUTPUT_FILE_PATH);
        return this;
    }

    /*public Swagger2MarkupFactory converter(URL outputFilePath){
        converter(localSwaggerFilePath,SwaggerConfig.OUTPUT_FILE_PATH);
        return this;
    }*/

    public static Swagger2MarkupFactory newInstance(){
        return new Swagger2MarkupFactory();
    }


}
